﻿using Microsoft.Extensions.DependencyInjection;
using Modules.System.User;
using PmSoft;

namespace Modules.System.Security
{
    [UseDI(ServiceLifetime.Scoped)]
    public class SecurityService
    {
        private readonly SecurityRepository securityRepository;

        public SecurityService(SecurityRepository securityRepository)
        {
            this.securityRepository = securityRepository;
        }

        public SysSecurity Create(SysSecurity sysSecurity)
        {
            securityRepository.Insert(sysSecurity);
            return sysSecurity;
        }

        public SysSecurity? GetSysSecurity(int id)
        {
            return securityRepository.Get(id);
        }

        public SysSecurity? GetSysSecurityByOpenId(string openId)
        {
            return securityRepository.GetSysSecurityByOpenId(openId);
        }

        public IEnumerable<SysSecurity> GetSysSecuritys(IEnumerable<int> enumerable)
        {
            return securityRepository.PopulateEntitiesByEntityIds(enumerable);
        }

        public SysSecurity Update(SysSecurity sysSecurity)
        {
            securityRepository.Update(sysSecurity);
            return sysSecurity;
        }

        public bool VerifyPassword(string password, int id) 
        {

            return GetSysSecurity(id)?.Password == SecurityPasswordHelper.EncodePassword(password, UserPasswordFormat.SM4) ;
        }
    }
}